<?php
namespace App\Process;

use App\Model\ShopsModel;
use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;
use App\Utility\Pool\RedisObject;
use App\Utility\Pool\RedisPool;
use App\Utility\RedisClient;
use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\EasySwoole\Logger;

class ShopOffline extends AbstractProcess{
    private $isRun = false;
    public function run($arg){
        //定时500ms检测有没有任务，有的话就while死循环执行
        $this->addTick(500,function (){
            if(!$this->isRun){
                $this->isRun = true;
                go(function (){
                    while (true){
                        $task = RedisPool::invoke(function (RedisObject $redis){
                            $redisClient = new RedisClient($redis);
                            return $redisClient->onQueue('shop-offline')->read();
                        });
                        if($task){
                            MysqlPool::invoke(function (MysqlObject $db) use($task){
                                $model  = new ShopsModel($db);
                                $shopInfo = $model->select('id')->where('fd',$task['fd'])->first();
                                if($shopInfo){
                                    $model->update(['id'=>$shopInfo['id'],'online'=>$model::ONLINE_2,'fd'=>0]);
                                }
                            });
                        }else{
                            break;
                        }
                    }
                    $this->isRun = false;
                });
            }
        });
    }

    public function onShutDown()
    {
        // TODO: Implement onShutDown() method.
    }

    public function onReceive(string $str, ...$args)
    {
        // TODO: Implement onReceive() method.
    }
}